package com.cms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service;

import com.cms.dao.mapper.LotteryMapper;
import com.cms.model.LotteryBets;
import com.cms.model.TrLotteryVo;
import com.cms.model.TrUser;
import com.cms.redis.RedisService;
import com.cms.service.LotteryService;
import com.cms.service.MemberUserService;
import com.cms.service.UserCoinLogService;

@Service
public class LotteryServiceImpl implements LotteryService {

	private static Logger logger =LoggerFactory.getLogger(LotteryServiceImpl.class);
	
	@Autowired
	private LotteryMapper lotteryMapper;

	@Autowired
	private UserCoinLogService userCoinService;

	@Autowired
	private MemberUserService userService;

	@Override
	public void updateLottery(TrLotteryVo lottery) throws Exception {
		try {
			lotteryMapper.updateLottery(lottery);
			String hash = lottery.getHash();// 获取彩种的hash
			RedisService.getRedisService().del(hash, "sscDataList");// 删除彩种数据//删除彩种类型数据
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/***
	 * 获取彩种信息
	 * 
	 * @param lottery
	 * @return
	 */
	public TrLotteryVo getLotteryInfo(TrLotteryVo lottery) {
		return lotteryMapper.getLotteryInfo(lottery);
	}

	@Override
	public void deleteLotteryBets(Map<String, Object> lotteryBet) throws Exception {
		LotteryBets bets = lotteryMapper.queryLotteryBet(lotteryBet);
		userCoinService.saveUserCoinLog(lotteryBet);
		// lotteryBet.put("actionAddr", SiteUtil.getActionAddr());
		lotteryMapper.updateLotteryBet(lotteryBet);
		TrUser user = userService.getUserInfoByUid(bets.getUid());
		user.setCoin(user.getCoin() + bets.getBetsAmount());
		this.userService.updateUserInfo(user);
	}

	@Override
	public void deleteLotteryBetsBatch(List<String> betsList) throws Exception {
		if (betsList != null && !betsList.isEmpty()) {
			Map<String, Object> lotteryMapper = new HashMap<String, Object>();
			for (String bets : betsList) {
				lotteryMapper.put("id", bets);
				this.deleteLotteryBets(lotteryMapper);
			}
		}
	}

}
